<?php

// extClass

class extClass{

    public $p1 = 'v1';

    function __construct($xxx=[]){
        //
    }

    static function getXlsSpheet($fp, $mode=1){
        ini_set('memory_limit', '960M'); // Xls
        $ext4 = strtolower(substr($fp,-4));
        $exmode = $ext4=='xlsx' ? 'Xlsx' : ($ext4=='.xls' ? 'Xls' : 'Xml');
        require_once DIR_VENDOR . '/autoload.php'; 
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($exmode);
        if($mode==2){
            $reader->setIncludeCharts(true);
        }else{
            $reader->setReadDataOnly(true);
        }
        $spreadsheet = $reader->load($fp); // 载入excel表格
        return $spreadsheet;
    }

    static function xlsDate($str, $re='-'){
        if(empty($str) || !intval($str)){ return $re; }
        if(preg_match("/^(\d{5}|\d{5}\.\d+)$/", $str)){ // echo " ((($str)))"; // 43180.876527778
            $sec = (intval($str) - 25569) * 3600 * 24; //转换成1970年以来的秒数
            return date('Y-m-d', $sec);
        }
        if(preg_match("/^\d{6}$/", $str)){ // echo " ((($str)))"; // 194704
            return substr($str,0,4).'-'.substr($str,4,2).'-'.substr($str,-2);
        }
        preg_match("/(\d+)((年|\.|\-)(\d+))?/", $str, $itms);
        if(empty($itms[1])){ return $re; }
        else{ $y4 = $itms[1]; }
        $m2 = empty($itms[4]) ? '1' : $itms[4]; if(strlen($m2)<2){ $m2 = "0$m2"; }
        return "$y4-$m2-01";
    }

    static function bingBgdo(){
        $hh = date('H'); // 0-23
        if(Basic::isLocal() && strpos("/09,15/",$hh)>0){
            $lists = glob(DIR_DATA."/remote/".date('Y-md-H')."*");
            if(count($lists)>0){ return; }
            echo " ##### bingDone ##### <br>\n";
            extClass::bingBgimg();
        }
    }

    // 获取`bing.com`背景图
    static function bingBgimg(){
        $base = 'https://cn.bing.com/';
        $bimg = 'https://s.cn.bing.net';
        $htm = Http::doCurl($base, 0);
        $htm = str_replace('&amp;', '&', $htm); $url = '';  
        preg_match("/ href\=\"([^\&]+)\&rf\=LaDigue_1920x1200\.jpg\" /i", $htm, $itm); 
        if(!empty($itm[1])){ $url = $itm[1]; }
        if($url){
            $ext = strrchr($url, '.'); 
            $ext = preg_replace("/[\&|\?]{1}\w+([^\"]+)/", '', $ext);
            $flg = Http::downSave("$bimg$url", DIR_DATA."/remote/".date('Y-md-His').$ext);
        }
    }

}
